﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BornShadow : MonoBehaviour {

    public static BornShadow instance;

    public Transform range_x1;
    public Transform range_x2;
    public Transform range_y1;
    public Transform range_y2;

    public GameObject bubblePre;

    public Transform ShadowPanelTrans;
    public Transform bubbleParentTrans;

    private List<Bubble> shadowBubbles;

    private void Awake()
    {
        instance = this;
        shadowBubbles = new List<Bubble>();
        shadowBubbles.Clear();
    }

    public void SpawnShadowBubble()
    {
        GameObject go = Instantiate(this.bubblePre);
        go.transform.SetParent(this.bubbleParentTrans);
        Bubble bubble = go.GetComponent<Bubble>();
        //Bubble bubble = BubbleSpawner.Instance.GetBubble();
        bubble.setOnStart = true;
        float x = Random.Range(this.range_x1.position.x, this.range_x2.position.x);
        float y = Random.Range(this.range_y1.position.y, this.range_y2.position.y);
        bubble.transform.position = new Vector3(x, y, 0);
        bubble.GetComponent<CircleCollider2D>().isTrigger = true;
        bubble.gameObject.AddComponent<BubbleShadow>();
        bubble.gameObject.GetComponent<Bubble>().enabled = false;
        bubble.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0.5f);
        bubble.transform.Find("Text").GetComponent<Text>().color = new Color(1, 1, 1, 0.5f);
        GameManager.Instance.SaveBubble(bubble);
        shadowBubbles.Add(bubble);
    }

    public void ShadowBubbleToReal()
    {
        if (shadowBubbles.Count != 0)
        {
            for (int i = 0; i < shadowBubbles.Count; i++)
            {
                Debug.Log("shadowbubblecount:" + shadowBubbles.Count);
                shadowBubbles[i].State = BubbleState.Live;
                shadowBubbles[i].GetComponent<CircleCollider2D>().isTrigger = false;
                shadowBubbles[i].GetComponent<SpriteRenderer>().color = Color.white;
                shadowBubbles[i].transform.Find("Text").GetComponent<Text>().color = Color.white;
            }
        }
    }
}
